#include "modulo.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QtCore>
#include <QGraphicsEffect>

Modulo::Modulo()
{
    visible = false;

    img_path = new QString(QApplication::applicationDirPath());

    modulo_img = new QImage(QString("%1/Images/modulo.png").arg(*img_path));
    setRect(0, 0, modulo_img->width(), modulo_img->height());
    setFlag(ItemIsMovable);

    itemScale = (float) QApplication::desktop()->height() / (float)1080;
    this->setScale(itemScale/4);
    this->setPos(600,50);
}

void Modulo::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->drawImage( option->exposedRect, *modulo_img, option->exposedRect, Qt::OrderedAlphaDither );
}

void Modulo::changeStatus()
{
    visible = !visible;

    if(visible){
        QPropertyAnimation *anim = new QPropertyAnimation(this, "scale");
        anim->setEasingCurve(QEasingCurve::OutCubic);
        anim->setDuration(500);
        anim->setStartValue(itemScale/4);
        anim->setEndValue(itemScale);
        anim->start();
    }
    else{
        QPropertyAnimation *anim = new QPropertyAnimation(this, "scale");
        anim->setEasingCurve(QEasingCurve::OutCubic);
        anim->setDuration(500);
        anim->setStartValue(itemScale);
        anim->setEndValue(itemScale/4);
        anim->start();
    }
}

void Modulo::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

}

void Modulo::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    changeStatus();
}
